package CRESTAURANT; 

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.DriverManager;

/**
 * Auxiliary class to manage the database.
 */
public class BDAccessor {

	@SuppressWarnings("unused")
	private String dbname;
	@SuppressWarnings("unused")
	private String host;
	@SuppressWarnings("unused")
	private String params;
	@SuppressWarnings("unused")
	private String port;
	@SuppressWarnings("unused")
	private String server;
	@SuppressWarnings("unused")
	private String user;
	@SuppressWarnings("unused")
	private String passwd;

	/**
	 * Default constructor
	 */
	public BDAccessor() {
		super();
		dbname = null;
		host = null;
		params = null;
		port = null;
		server = null;
		user = null;
		passwd = null;
	}

	/**
	 * Constructor with all parameters.
	 * @param host Server name with the DB.
	 * @param port DB server port number.
	 * @param dbname DB name.
	 * @param server Server name.
	 * @param params Additional parameters.
	 * @param user User name for the connection.
	 * @param passwd User password.
	 */
	public BDAccessor(
			String host,
			String port,
			String dbname,
			String server,
			String params,
			String user,
			String passwd) {
		this.host = host;
		this.port = port;
		this.dbname = dbname;
		this.server = server;
		this.params = params;
		this.user = user;
		this.passwd = passwd;
	}

	/**
	 * Maps a properties file to a Properties object
	 * @param file Properties file name
	 * @return Properties Properties object with the info from the file 
	 * @throws IOException Occurs if the file is not found
	 */
	private static Properties loadParams( String file )
		throws IOException {
		Properties prop = new Properties();
		prop.load(new FileInputStream (file));
		return prop;
	}

	/**
	 * Obtains a connection to the database, reading the properties
	 * file (bd.properties) previously.
	 * @return DB connection or null if a problem occurred when trying to connect.
	 */
	public Connection getConnection() {
	    Connection con = null;
	    
		try {
    		// Read the DB configuration file.
			this.loadDBParams();

     		// Loads the driver
			Class.forName( "com.informix.jdbc.IfxDriver" );

    		// Connects
			String url = "jdbc:informix-sqli://"+host+":"+port+"/"+dbname+":"+server+";user="+user+";password="+passwd+";";
			con = DriverManager.getConnection( url);
			
			con.setAutoCommit( false );
		} catch (IOException e) {
			System.err.println( "ERROR: Parameters file could not be read" );
			System.err.println( e.getMessage() );
		} catch (ClassNotFoundException e1) {
			System.err.println( "ERROR: Not possible to load the JDBC driver" );
			System.err.println( e1.getMessage() );
		} catch ( SQLException e2 ) {
			System.err.println( "ERROR: Not possible to connect with the DB" );
			System.err.println( e2.getMessage() );
		}

		return con;
	}

	/**
	 * Closes the DB connection.
	 * @param con Connection to the DB.
	 */
	public static void closeConnection( Connection con ) {
		if ( con != null ) {
			try {
				con.close();
			} catch ( SQLException e ) {
				System.err.println( "Error closing the DB connection" );
				System.err.println( e.getMessage() );
			}
		}
	}

	/**
	 * Load the properties file for the database and assigns them to 
	 * the class attributes
	 * @throws IOException Properties file could not be found.
	 */
	@SuppressWarnings("unused")
	private void loadDBParams()
		throws IOException {
		// Get the database configuration
		Properties prop = null;
		try {
			prop = loadParams( Utilities.getResourceURL("bd.properties"));
			this.host   = prop.getProperty( "host" );
			this.port   = prop.getProperty( "port" );
			this.dbname = prop.getProperty( "dbname" );
			this.server = prop.getProperty( "server" );
			this.params = prop.getProperty( "params" );
			this.user   = prop.getProperty( "user" );
			this.passwd = prop.getProperty( "passwd" );
		}
		catch ( IOException e ) {
			System.err.println( "ERROR: bd.properties file could not be found");
			throw e;
		}
	}
}